import { Controller } from 'egg';

class UserController extends Controller {
    /**
     * 处理后台登录
     * @returns {Promise<void>}
     */
    public async login() {
        const { ctx, ctx: { request } } = this;
        try {
            ctx.validate({
                admin_username: 'string',
                admin_password: 'string',
            }, request.body)
        } catch (e) {
            ctx.body = ctx.helper.responseErrorParam(e.errors[0].message);
            return;
        }
        const param = request.body;
        const returnData: any = await this.service.adminService.login(param.admin_username, param.admin_password);
        const token = ctx.helper.genUUID();
        await this.updateToken(returnData.admin_id, token);
        ctx.body = { code: 1, msg: '登录成功', data: { token, admin_username: returnData.admin_username } };
    }

    /**
     * 登录成功后刷新token值
     * @param {string} admin_id
     * @param {string} token
     * @returns {Promise<void>}
     */
    public async updateToken(admin_id: string, token: string) {
        await this.service.adminService.updateToken(admin_id, token);
    }

    /**
     * 新增管理员
     */
    public async createAdmin() {
        const { ctx, ctx: { request } } = this;
        try {
            ctx.validate({
                admin_username: 'string',
                admin_password: 'string',
                admin_phone:'string',
                admin_code:'string'
            }, request.body)
        } catch (e) {
            ctx.body = ctx.helper.responseErrorParam(e.errors[0].message);
            return;
        }
        await this.ctx.service.adminService.createAdmin(request.body);
        ctx.body = ctx.helper.responseSuccess('新增成功');
    }

    /**
     * 查找管理员
     */
    public async searchAdmin() {
        const { ctx } = this;
        const admin = await this.ctx.service.adminService.searchAdmin();
        ctx.body = ctx.helper.responseSuccess('查询成功', admin);
    }

    /**
     * 修改管理员
     */
    public async updateAdmin() {
        const { ctx, ctx: { request } } = this;
        try {
            ctx.validate({
                target_admin_id:'string',
                target_admin_password: 'string'
            }, request.body)
        } catch (e) {
            ctx.body = ctx.helper.responseErrorParam(e.errors[0].message);
            return;
        }
        await this.ctx.service.adminService.udpateAdmin(request.body);
        ctx.body = ctx.helper.responseSuccess('修改成功');
    }

    /**
     * 重绑手机号
     */
    public async udpateAdminPhone() {
        const { ctx, ctx: { request } } = this;
        try {
            ctx.validate({
                target_admin_id:'string',
                admin_phone: 'string',
                admin_code:'string'
            }, request.body)
        } catch (e) {
            ctx.body = ctx.helper.responseErrorParam(e.errors[0].message);
            return;
        }
        await this.ctx.service.adminService.udpateAdminPhone(request.body);
        ctx.body = ctx.helper.responseSuccess('修改成功');
    }

    /**
     * 启用、停用管理员
     */
    public async startAndStopAdmin() {
        const { ctx, ctx: { request } } = this;
        try {
            ctx.validate({
                target_admin_id:'string',
                type: 'type',
            }, request.body)
        } catch (e) {
            ctx.body = ctx.helper.responseErrorParam(e.errors[0].message);
            return;
        }
        await this.ctx.service.adminService.startAndStopAdmin(request.body);
        ctx.body = ctx.helper.responseSuccess('操作成功');
    }

    /**
     * 发送验证码
     */
    public async sendSMS(){
        const { ctx, ctx: { request } } = this;
        try {
            ctx.validate({
                admin_username:'string'
            }, request.body)
        } catch (e) {
            ctx.body = ctx.helper.responseErrorParam(e.errors[0].message);
            return;
        }
        await this.ctx.service.adminService.sendSMS(request.body);
        ctx.body = ctx.helper.responseSuccess('发送成功');
    }

    /**
     * 找回密码
     */
    public async forgetPassword(){
        const { ctx, ctx: { request } } = this;
        try {
            ctx.validate({
                admin_username:'string',
                admin_password:'string',
                code:'string'
            }, request.body)
        } catch (e) {
            ctx.body = ctx.helper.responseErrorParam(e.errors[0].message);
            return;
        }
        await this.ctx.service.adminService.forgetPassword(request.body);
        ctx.body = ctx.helper.responseSuccess('修改成功');
    }

}

export default UserController;
